package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import com.atguigu.common.exception.BizExceptionEnum;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegisterVo;
import com.atguigu.gulimall.member.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 会员
 *
 * @author zr
 * @email zr@gmail.com
 * @date 2022-08-27 10:51:06
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo registerVo) {
        try {
            memberService.register(registerVo);
        } catch (UsernameExistException e) {
            return R.error(BizExceptionEnum.USER_EXIST_EXCEPTION.getCode(), BizExceptionEnum.USER_EXIST_EXCEPTION.getMsg());
        } catch (PhoneExistException e) {
            return R.error(BizExceptionEnum.PHONE_EXIST_EXCEPTION.getCode(), BizExceptionEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (Objects.nonNull(memberEntity)) {
            return R.ok().setData( memberEntity);
        } else {
            return R.error(BizExceptionEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizExceptionEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }
    @PostMapping("/oAuth/login")
    public R oAuthLogin(@RequestBody SocialUserVo socialUserVo) {
        MemberEntity memberEntity = memberService.oAuthLogin(socialUserVo);
        if (Objects.nonNull(memberEntity)) {
            return R.ok().setData( memberEntity);
        } else {
            return R.error(BizExceptionEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizExceptionEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }
    @GetMapping("/coupon/list")
    public R couponList() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("李四");
        return Objects.requireNonNull(R.ok().put("members", memberEntity)).
                put("coupons", couponFeignService.memberList().get("coupon"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
